<?php
App::uses('AppModel', 'Model');
/**
 * UserOrderList Model
 *
 */
class UserOrderList extends AppModel {

/**
 * Use table
 *
 * @var mixed False or table name
 */
	public $useTable = 'user_order_list';

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'user_order_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ticket_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ticket_price_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'price_sigle' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'price_num' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'price_total' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	
	public $belongsTo = array(
			'Ticket' => array(
					'className' => 'Ticket',
					'foreignKey' => 'ticket_id',
					'conditions' => '',
					'fields' => '',
					'order' => ''
			),
			'TicketPrice' => array(
					'className' => 'TicketPrice',
					'foreignKey' => 'ticket_price_id',
					'conditions' => '',
					'fields' => '',
					'order' => ''
			)
			
		 );
	
	public function checkTicketSeat($ticket_showing_id,$seat_id){
		$this->recursive = -1;
		$check = $this->find('first',array(
				'fields'=>array('UserOrderList.id'),
				'conditions'=>array(
						'UserOrderList.ticket_showing_id'=>$ticket_showing_id,
						'UserOrderList.ticket_seat_id'=>$seat_id,
						'OR'=>array(
								'AND'=>array(
										'UserOrderList.ticket_seat_sold_id'=>1,
										'UserOrderList.created <='=>date('Y-m-d H:i:s',time()+30*60)
								),
								'UserOrderList.ticket_seat_sold_id'=>2
						)
				)
		));
		if($check){
			return true;
		}else{
			return false;
		}
	}
}
